"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】修改事件触发器的所有者
Create At   :   2024/07/15
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0053()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        2。创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0053 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0053();
        3.创建新用户
        CREATE USER u_eventtrigger_0053 sysadmin PASSWORD 'Bigdata@123';
        CREATE USER u_eventtrigger_0053_1 PASSWORD 'Bigdata@123';
    测试步骤：
        1.修改所有者
        ALTER EVENT TRIGGER eventtri_eventtrigger_0053 OWNER TO u_eventtrigger_0053;
        2.查看修改后所有者
        \dy eventtri_eventtrigger_0053
        3、修改所有者为普通用户u_eventtrigger_0053_1
        ALTER EVENT TRIGGER eventtri_eventtrigger_0053 OWNER TO u_eventtrigger_0053_1;
        4、修改所有者为当前用户
        alter event trigger eventtri_eventtrigger_0053 owner to  {self.userNode.ssh_user};
        5、\dy查看修改后owner
Expect      : 
        1.修改成功
        2.Owner为u_eventtrigger_0053
        3、修改失败不允许修改
        4、修改成功
        5、owner为{self.userNode.ssh_user}
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.userNode = self.comsh.node
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0053;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0053();")
        self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0053;")
        self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0053_1;")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0053() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0053 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0053();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("CREATE USER u_eventtrigger_0053 sysadmin PASSWORD 'Bigdata@123';")
        self.logger.info(msg)
        self.assertEqual(f'CREATE ROLE', msg)
        msg = self.comsh.execut_db_sql("CREATE USER u_eventtrigger_0053_1 PASSWORD 'Bigdata@123';")
        self.logger.info(msg)
        self.assertEqual(f'CREATE ROLE', msg)

    def test_Index_file_damaged(self):
        self.logger.info("修改事件触发器owner")
        msg = self.comsh.execut_db_sql("ALTER EVENT TRIGGER eventtri_eventtrigger_0053 OWNER TO u_eventtrigger_0053;")
        self.logger.info(msg)
        self.assertEqual(f'ALTER EVENT TRIGGER', msg)
        self.logger.info("查看修改后所有者")
        msg = self.comsh.execut_db_sql(f"\dy eventtri_eventtrigger_0053")
        self.logger.info(msg)
        self.assertIn(f'eventtri_eventtrigger_0053 | ddl_command_start | u_eventtrigger_0053 | enabled', msg)
        self.logger.info("修改事件触发器所有者为普通用户u_eventtrigger_0053_1")
        msg = self.comsh.execut_db_sql("ALTER EVENT TRIGGER eventtri_eventtrigger_0053 OWNER TO u_eventtrigger_0053_1;")
        self.logger.info(msg)
        self.assertEqual(
            f'ERROR:  permission denied to change owner of event trigger "eventtri_eventtrigger_0053"\nHINT:  The owner of an event trigger must be a superuser.',
            msg)
        self.logger.info("修改事件触发器owner")
        msg = self.comsh.execut_db_sql(
            f"alter event trigger eventtri_eventtrigger_0053 owner to  {self.userNode.ssh_user};")
        self.logger.info(msg)
        self.assertEqual(f'ALTER EVENT TRIGGER', msg)
        self.logger.info("查看修改后所有者")
        msg = self.comsh.execut_db_sql(f"\dy eventtri_eventtrigger_0053")
        self.logger.info(msg)
        self.assertIn(f'eventtri_eventtrigger_0053 | ddl_command_start | {self.userNode.ssh_user} | enabled', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0053;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0053();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        # 删除用户
        msg = self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0053;")
        self.logger.info(msg)
        self.assertEqual('DROP ROLE', msg)
        msg = self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0053_1;")
        self.logger.info(msg)
        self.assertEqual('DROP ROLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
